package com.dyh.controller;

//引用area实体类
import com.dyh.po.area;
//引用areaService服务层
import com.dyh.service.areaService;
//引用spring相关组件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
//使用List数据结构
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * areaController控制器，实现对区域管理用例的控制
 */
@Controller
public class areaController {
    @Autowired
    areaService areaservice;

    /**
     * 查找所有行政区域
     * @param model
     * @return List
     */
    @RequestMapping(value = "areaList")
    public String areaList(Model model){
        List<area> areas = areaservice.findAllArea();
        model.addAttribute("areaList",areas);
        return "area/arealist";
    }

    /**
     * 跳转到行政区域添加页
     * @return String
     */
    @RequestMapping(value = "areaAdd")
    public String toAreaAdd(){
        return "area/areaAdd";
    }

    /**
     * 查找所有行政区域
     * @return Object
     */
    @RequestMapping(value = "findAllArea")
    @ResponseBody
    public Object findAllArea(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("arealist",areaservice.findAllArea());
        return map;
    }

    @RequestMapping(value = "areaEdit")
    public ModelAndView toAreaEdit(HttpServletRequest request){
        String areaId = request.getParameter("a_id");
        area area=areaservice.findAreaById(areaId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("area",area);
        mv.setViewName("area/areaEdit");
        return mv;
    }

    @RequestMapping(value = "editArea")
    public String editArea(HttpServletRequest request){
        String id = request.getParameter("id");
        String areaname = request.getParameter("areaname");
        return null;
    }

    @RequestMapping(value = "areaDeleteAction")
    public String deleteArea(HttpServletRequest request){
        String areaId = request.getParameter("a_id");
        int row = areaservice.deleteArea(areaId);
        return "redirect:/areaList";
    }
}
